package study.mybatis.support.utils;

import java.lang.reflect.Field;
import java.util.Arrays;

public class FieldUtils {

    /**
     * 是否包含属性
     */
    public static boolean contain(Object obj, String name) {
        boolean flg = false;
        if (obj == null || name == null) {
            return flg;
        }
        Field[] fields = obj.getClass().getDeclaredFields();
        flg = Arrays.stream(fields)
                .anyMatch(e -> e.getName().equals(name));
        return flg;
    }

    /**
     * 给属性赋值
     */
    public static void setValue(Object obj, String name, Object value) {
        if (obj == null || value == null) {
            return;
        }
        try {
            Field field = obj.getClass().getDeclaredField(name);
            field.setAccessible(true);
            field.set(obj, value);
        } catch (NoSuchFieldException | IllegalAccessException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 获取属性值
     */
    public static <T> T getValue(Object obj, String name) {
        return null;
    }

}
